import { userAPI, authAPI } from '@/api'
import { ElMessage } from 'element-plus'

// API 测试工具
export const apiTest = {
  // 测试用户登录
  async testUserLogin() {
    try {
      const testCredentials = {
        username: 'test@example.com',
        password: '123456'
      }
      
      console.log('🧪 测试用户登录接口...')
      const response = await userAPI.login(testCredentials)
      console.log('✅ 用户登录接口正常:', response)
      return { success: true, data: response }
    } catch (error) {
      console.error('❌ 用户登录接口失败:', error)
      return { success: false, error: error.message }
    }
  },

  // 测试用户列表
  async testUserList() {
    try {
      console.log('🧪 测试用户列表接口...')
      const response = await userAPI.getList()
      console.log('✅ 用户列表接口正常:', response)
      return { success: true, data: response }
    } catch (error) {
      console.error('❌ 用户列表接口失败:', error)
      return { success: false, error: error.message }
    }
  },

  // 测试用户分页查询
  async testUserPage() {
    try {
      const pageData = {
        page: 1,
        size: 10,
        // 根据后端 UserQueryDto 添加查询条件
        username: '',
        email: '',
        phone: '',
        userType: ''
      }
      
      console.log('🧪 测试用户分页查询接口...')
      const response = await userAPI.getPage(pageData)
      console.log('✅ 用户分页查询接口正常:', response)
      return { success: true, data: response }
    } catch (error) {
      console.error('❌ 用户分页查询接口失败:', error)
      return { success: false, error: error.message }
    }
  },

  // 测试用户创建
  async testUserCreate() {
    try {
      const testUserData = {
        username: 'testuser',
        email: 'test@example.com',
        phone: '13800138000',
        password: '123456',
        realName: '测试用户',
        userType: 'user'
      }
      
      console.log('🧪 测试用户创建接口...')
      const response = await userAPI.create(testUserData)
      console.log('✅ 用户创建接口正常:', response)
      return { success: true, data: response }
    } catch (error) {
      console.error('❌ 用户创建接口失败:', error)
      return { success: false, error: error.message }
    }
  },

  // 测试用户更新
  async testUserUpdate() {
    try {
      const testUpdateData = {
        id: 1, // 需要真实存在的用户ID
        username: 'updateduser',
        email: 'updated@example.com',
        phone: '13800138001',
        realName: '更新用户'
      }
      
      console.log('🧪 测试用户更新接口...')
      const response = await userAPI.update(testUpdateData)
      console.log('✅ 用户更新接口正常:', response)
      return { success: true, data: response }
    } catch (error) {
      console.error('❌ 用户更新接口失败:', error)
      return { success: false, error: error.message }
    }
  },

  // 测试用户详情
  async testUserDetail(userId = 1) {
    try {
      console.log('🧪 测试用户详情接口...')
      const response = await userAPI.getInfo(userId)
      console.log('✅ 用户详情接口正常:', response)
      return { success: true, data: response }
    } catch (error) {
      console.error('❌ 用户详情接口失败:', error)
      return { success: false, error: error.message }
    }
  },

  // 测试用户删除
  async testUserDelete(userId = 1) {
    try {
      console.log('🧪 测试用户删除接口...')
      const response = await userAPI.delete(userId)
      console.log('✅ 用户删除接口正常:', response)
      return { success: true, data: response }
    } catch (error) {
      console.error('❌ 用户删除接口失败:', error)
      return { success: false, error: error.message }
    }
  },

  // 运行所有用户相关测试
  async runAllUserTests() {
    console.log('🚀 开始用户接口测试...')
    
    const results = {
      login: await this.testUserLogin(),
      list: await this.testUserList(),
      page: await this.testUserPage(),
      create: await this.testUserCreate(),
      update: await this.testUserUpdate(),
      detail: await this.testUserDetail(),
      delete: await this.testUserDelete()
    }
    
    console.log('📊 用户接口测试结果:', results)
    
    const allPassed = Object.values(results).every(result => result.success)
    if (allPassed) {
      ElMessage.success('🎉 所有用户接口测试通过!')
    } else {
      ElMessage.warning('⚠️ 部分用户接口测试失败，请检查后端服务')
    }
    
    return results
  },

  // 测试后端连接
  async testBackendConnection() {
    try {
      console.log('🧪 测试后端连接...')
      
      // 尝试获取用户列表作为连接测试
      const response = await userAPI.getList()
      
      console.log('✅ 后端连接正常')
      ElMessage.success('后端连接正常')
      return { success: true, data: response }
    } catch (error) {
      console.error('❌ 后端连接失败:', error)
      ElMessage.error('后端连接失败，请检查服务器状态')
      return { success: false, error: error.message }
    }
  }
}

// 导出测试函数
export default apiTest 